﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.StockManage
{
    public partial class PurchaseView : AuthBasePage
    {
        #region 属性定义
        private string m_strTbl = "BS_Purchase";
        private string m_strMainKey = "BillID";

        private DataTable DtPurchaseData
        {
            get
            {
                if (null == Session["PurchaseData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["PurchaseData"];
                }
            }
            set
            {
                Session["PurchaseData"] = value;
            }
        }

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BillDetailID");//0
            dt.Columns.Add("OrderNum");//1
            dt.Columns.Add("RquisitionDetailID");//2
            dt.Columns.Add("RquisitionID");//3
            dt.Columns.Add("WorkOrderNo");//4
            dt.Columns.Add("MaterialID");//5
            dt.Columns.Add("MaterialName");//6
            dt.Columns.Add("Specifications");//7
            dt.Columns.Add("Unit");//8
            dt.Columns.Add("RquisitionNums");//需求数量
            dt.Columns.Add("ResNum");//剩余数量
            dt.Columns.Add("Nums");//11
            dt.Columns.Add("MMNumber");//11
            dt.Columns.Add("SingleMMNumber");//11
            dt.Columns.Add("Price");//12
            dt.Columns.Add("Amount");//13
            dt.Columns.Add("ExpectDate");//14
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }
        #endregion

        #region 初始化及绑定控件
        private void InitData()
        {
            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            if (!string.IsNullOrEmpty(id))
            {
                DataTable dt = BsPurchaseBusiness.GetDataByBillID(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow d = dt.Rows[0];
                    this.lblBillID.Text = id;
                    this.lblBillDate.Text = string.Format("{0:yyyy-MM-dd}", d["BillDate"]);
                    this.txtSupplierID.Text = CommonMethod.GetString(d["SupplierID"]);
                    this.txtSupplierName.Text = CommonMethod.GetString(d["SupplierName"]);

                    DataTable dtemp = DtPurchaseData.Copy();
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow drow = dtemp.NewRow();
                        drow["BillDetailID"] = CommonMethod.GetString(dr["BillDetailID"]);
                        drow["OrderNum"] = CommonMethod.GetString(dr["OrderNum"]);
                        drow["RquisitionDetailID"] = CommonMethod.GetString(dr["RquisitionDetailID"]);
                        drow["RquisitionID"] = CommonMethod.GetString(dr["RquisitionID"]);
                        drow["WorkOrderNo"] = CommonMethod.GetString(dr["WorkOrderNo"]);
                        drow["MaterialID"] = CommonMethod.GetString(dr["MaterialID"]);
                        drow["MaterialName"] = CommonMethod.GetString(dr["MaterialName"]);
                        drow["Specifications"] = CommonMethod.GetString(dr["Specifications"]);
                        drow["Unit"] = CommonMethod.GetString(dr["Unit"]);
                        drow["RquisitionNums"] = CommonMethod.GetString(dr["RquisitionNums"]);
                        drow["ResNum"] = CommonMethod.GetString(dr["ResNum"]);
                        drow["Nums"] = CommonMethod.GetString(dr["Nums"]);
                        drow["MMNumber"] = CommonMethod.GetString(dr["MMNumber"]);
                        drow["SingleMMNumber"] = CommonMethod.GetString(dr["SingleMMNumber"]);
                        drow["Price"] = CommonMethod.GetString(dr["Price"]);
                        drow["Amount"] = CommonMethod.GetString(dr["Amount"]);
                        drow["ExpectDate"] = string.Format("{0:yyyy-MM-dd}", dr["ExpectDate"]);
                        drow["IsNew"] = "-1";
                        drow["IsDel"] = "-1";
                        dtemp.Rows.Add(drow);
                    }
                    dtemp.AcceptChanges();
                    DtPurchaseData = dtemp.Copy();
                }
            }
            BindData();
        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DtPurchaseData = null;
                grdResult.Style.Add("word-break", "keep-all");
                grdResult.Style.Add("word-wrap", "normal");
                InitData();
            }
            Master.ValidationGroup = "gSubmit";
            UIHelper.RunScript(this.UpdatePanel1, "Refreashtab();");
        }

        private void BindData()
        {
            DataView dv = new DataView(DtPurchaseData);
            dv.RowFilter = " IsDel <> '1' ";
            DataTable dt = dv.ToTable();
            if (dt != null && dt.Rows.Count > 0)
            {
                int index = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    dr["OrderNum"] = index;
                    index++;
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }
        #endregion
    }
}
